package com.jixiantubu.adapter;

import image.SmartImageView;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.jixiantubu.bean.Photo_library;
import com.jixiantubu.util.MyApplication;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;



public class JinNang_YouJi_JuTiNeiRong_TuPianAdapter extends BaseAdapter {
	Context context;
	List<Photo_library> list = new ArrayList<Photo_library>();
	LayoutInflater mInflater;
	ViewHolder holder;
	GridView gridView;
	int count, width;
	// 这里注意LayoutParams是属于AbsListView类的，因为本例中图片父布局是gridview
	LayoutParams params;
	private MyPagerAdapter_youji pageradapter;
	
	public JinNang_YouJi_JuTiNeiRong_TuPianAdapter(Context context,
			List<Photo_library> list, GridView gridView,
			int width,MyPagerAdapter_youji pageradapter) {
		super();
		this.context = context;
		this.list = list;
		this.gridView = gridView;
		this.width = width;
		this.pageradapter = pageradapter;
		mInflater = LayoutInflater.from(context);
	}

	public List<Photo_library> getList() {
		return list;
	}

	public void setList(List<Photo_library> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// Log.e("!!!胡椒", list.toString());
		if (arg1 == null) {
			holder = new ViewHolder();
			arg1 = mInflater.inflate(
					R.layout.jinnang_youji_jutineirong_tupian_item, null);
			holder.imageView = (SmartImageView) arg1
					.findViewById(R.id.jinang_youji_jutineirong_tpiv);
			/*
			 * gridView.setNumColumns(1); params=new LayoutParams(width,
			 * width/2); holder.imageView.setLayoutParams(params);
			 */
			if (count == 1) {
				// 设置gridview每行只显示一张图片
				gridView.setNumColumns(1);
				// 只有一张图片时，图片宽与手机屏幕分辨率宽一致，高为宽分辨率的一半
				params = new LayoutParams(width, width / 2);
				holder.imageView.setLayoutParams(params);
			} else if (count == 2) {
				// 设置gridview每行只显示2张图片
				gridView.setNumColumns(2);
				// 只有两张图片时，图片宽度几乎为手机分辨率宽的一半，因为gridview设置了
				// 两张图片间距为1dp，所以这里减去了一个值3，可以自行调节此值大小
				params = new LayoutParams(width / 2 - 3, width / 3);
				holder.imageView.setLayoutParams(params);
			} else {
				// 设置gridview每行只显示3张图片
				gridView.setNumColumns(3);
				params = new LayoutParams(width / 3 - 3, width / 4);
				holder.imageView.setLayoutParams(params);
			}
			arg1.setTag(holder);
		} else {
			holder = (ViewHolder) arg1.getTag();
		}
		String url = "http://" + MyApplication.getIP() + ":8080/Jinnang/img/"
				+ list.get(arg0).getpl_path();
		Log.e("!!!胡椒", url);
		holder.imageView.setImageUrl(url, R.drawable.error, R.drawable.loding);
		
		/*holder.imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 定义一个数组，存放图片路径
				if (list.size() != 0) {
					// 当图片路径不为0时，传给数组
					String[] images = new String[list.size()];
					for (int i = 0; i < list.size(); i++) {
						images[i] = "http://" + MyApplication.getIP()
								+ ":8080/Jinnang/img/"
								+ list.get(i).getpl_path();
						Log.e("图片路径", images[i].toString());
					}
					Intent intent = new Intent(context,
							ImagePagerActivity.class);
					// 图片url,一般从数据库中或网络中获取
					intent.putExtra("images", images);
					context.startActivity(intent);
				}
			}
		});
		*/
		return arg1;
	}

}

class ViewHolder {
	SmartImageView imageView;

}
